Skip to main content
Version: 26.2.0

DateFormatter

The DateFormatter class provides flexible date and time formatting with support for both absolute date formatting (using custom format strings) and indexed date formatting (for time-series indices like day of week, month of year, etc.).

Constructor

new DateFormatter(options)

Parameters

PropertyTypeRequiredDescription
optionsAbsoluteDateFormatterOptions | IndexedDateFormatterOptionstrueFormatting options (type determined by presence of format or timeIndex property)

Formatting Modes

Absolute Date Formatting

Format dates using custom format strings based on Luxon.js token standards. This mode is used when you provide a format property.

const formatter = new DateFormatter({
  format: 'MMM dd, yyyy'  // Luxon format string
});

const formatted = formatter.format(new Date('2024-01-15'));
// Output: "Jan 15, 2024"

Absolute Date Options

OptionTypeDefaultDescription
formatstring'dd/MM/yyyy'Luxon format string for date formatting

Common Format Tokens

TokenMeaningExample
yyyy4-digit year2024
yy2-digit year24
MMMMFull month nameJanuary
MMMAbbreviated month nameJan
MM2-digit month01
MMonth1
dd2-digit day05
dDay5
EEEEFull weekday nameMonday
EEEAbbreviated weekday nameMon
HH2-digit hour (24h)14
hh2-digit hour (12h)02
mm2-digit minute30
ss2-digit second45
aAM/PMPM

Examples

// Full date with time
const fullFormatter = new DateFormatter({
  format: 'MMMM dd, yyyy HH:mm:ss'
});
const formatted = fullFormatter.format(new Date('2024-01-15T14:30:45'));
// Output: "January 15, 2024 14:30:45"

// Short date
const shortFormatter = new DateFormatter({
  format: 'MM/dd/yy'
});
const formatted = shortFormatter.format(new Date('2024-01-15'));
// Output: "01/15/24"

// Month and year only
const monthYearFormatter = new DateFormatter({
  format: 'MMM yyyy'
});
const formatted = monthYearFormatter.format(new Date('2024-01-15'));
// Output: "Jan 2024"

// Weekday and date
const weekdayFormatter = new DateFormatter({
  format: 'EEE, MMM d'
});
const formatted = weekdayFormatter.format(new Date('2024-01-15'));
// Output: "Mon, Jan 15"

// Time only
const timeFormatter = new DateFormatter({
  format: 'HH:mm'
});
const formatted = timeFormatter.format(new Date('2024-01-15T14:30:00'));
// Output: "14:30"

// 12-hour format with AM/PM
const time12hFormatter = new DateFormatter({
  format: 'hh:mm a'
});
const formatted = time12hFormatter.format(new Date('2024-01-15T14:30:00'));
// Output: "02:30 PM"

// ISO format
const isoFormatter = new DateFormatter({
  format: 'yyyy-MM-dd'
});
const formatted = isoFormatter.format(new Date('2024-01-15'));
// Output: "2024-01-15"

Indexed Date Formatting

Format time indices as human-readable labels. This mode is used when you provide a timeIndex property instead of format.

const formatter = new DateFormatter({
  timeIndex: 'dayOfWeek'
});

const formatted = formatter.format(1);
// Output: "Sunday"

const formatted = formatter.format(2);
// Output: "Monday"

Indexed Date Options

OptionTypeDefaultDescription
timeIndexTimeIndex'none'The type of time index to format

TimeIndex Values

ValueDescriptionInput RangeOutput
'none'No formatting (returns numeric value)Any numberString representation of the number
'hourOfDay'Hour of the day0-23Numeric string
'dayOfWeek'Day of the week1-7"Sunday", "Monday", etc.
'dayOfMonth'Day of the month1-31Numeric string
'dayOfQuarter'Day of the quarter1-92Numeric string
'dayOfYear'Day of the year1-366Numeric string
'weekOfMonth'Week of the month1-5Numeric string
'weekOfQuarter'Week of the quarter1-13Numeric string
'weekOfYear'Week of the year1-53Numeric string
'monthOfQuarter'Month of the quarter1-3Numeric string
'monthOfYear'Month of the year1-12"January", "February", etc.
'quarterOfYear'Quarter of the year1-4"Q1", "Q2", "Q3", "Q4"

Examples

// Day of week
const dayOfWeekFormatter = new DateFormatter({
  timeIndex: 'dayOfWeek'
});
const formatted = dayOfWeekFormatter.format(1);  // "Sunday"
const formatted = dayOfWeekFormatter.format(7);  // "Saturday"

// Month of year
const monthFormatter = new DateFormatter({
  timeIndex: 'monthOfYear'
});
const formatted = monthFormatter.format(1);   // "January"
const formatted = monthFormatter.format(12);  // "December"

// Quarter of year
const quarterFormatter = new DateFormatter({
  timeIndex: 'quarterOfYear'
});
const formatted = quarterFormatter.format(1);  // "Q1"
const formatted = quarterFormatter.format(4);  // "Q4"

// Numeric indices
const dayOfMonthFormatter = new DateFormatter({
  timeIndex: 'dayOfMonth'
});
const formatted = dayOfMonthFormatter.format(15);  // "15"

const weekOfYearFormatter = new DateFormatter({
  timeIndex: 'weekOfYear'
});
const formatted = weekOfYearFormatter.format(26);  // "26"

Methods

format()

Formats a date value and returns the formatted string.

format(value: MuzeDatum): string

Parameters:

PropertyTypeRequiredDescription
valueDate | number | MuzeDatumtrueThe date value to format (Date object, timestamp, or time index)

Returns: String - The formatted date

formatterFunc()

Returns a formatter function for use with Muze visualizations.

formatterFunc(): (dataInfo: MuzeType) => string

Returns: Function - A formatter function that can be passed to Muze configuration